package com.ton.common.service;

import com.kypj.frame.db.MyTemplate;
import com.kypj.frame.db.Pager;
import com.ton.common.controller.order.req.QueryOrderReq;
import com.ton.common.entity.TonPayOrder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class PayOrderService {

    @Autowired
    private MyTemplate myTemplate;


    /**
     * 订单保存
     *
     * @param tonPayOrder
     */
    public void save(TonPayOrder tonPayOrder) {
        myTemplate.mySaveObject(tonPayOrder);
    }

    /**
     * 根据ID查找支付订单
     *
     * @param id 支付订单ID
     * @return PayOrder 订单对象
     */
    public TonPayOrder getPayOrderById(Long id) {
        return myTemplate.myGetObjectAppendWhere(TonPayOrder.class, "id=?", id);
    }

    /**
     * 根据merchantOrderId查找支付订单
     *
     * @param id 支付订单ID
     * @return PayOrder 订单对象
     */
    public TonPayOrder getPayOrderMerchantId(String id) {
        return myTemplate.myGetObjectAppendWhere(TonPayOrder.class, "merchant_order_id=?", id);
    }

    public void updatePayOrder(TonPayOrder tonPayOrderUpdate) {
        myTemplate.myUpdateObject(tonPayOrderUpdate);
    }

    /**
     * 管理端提供的 分页查询订单的接口
     *
     * @param queryOrderReq
     * @param pager
     */
    public List<TonPayOrder> queryList(QueryOrderReq queryOrderReq, Pager pager) {
        pager.setCount(myTemplate.myCountAppendWhere(TonPayOrder.class, null));
        return myTemplate.myQueryForListAppendWhereCondition(TonPayOrder.class, "1=1 order by create_time desc limit ?,?", pager.limitStart(), pager.limitEnd());
    }

    /**
     * 订单完成后更新的状态
     *
     * @param tonPayOrder           订单对象
     * @param balanceTrx         trx余额
     * @param balanceTrc20       trc20余额
     */
    @Transactional
    public void updateCompletionStatus(TonPayOrder tonPayOrder, Long balanceTrx, Long balanceTrc20) {
        TonPayOrder tonPayOrderUpdate = getPayOrderById(tonPayOrder.getId());
        if (tonPayOrderUpdate.getStatus() != 1) {
            tonPayOrderUpdate.setStatus(1); //状态设为已完成
            updatePayOrder(tonPayOrderUpdate);

        }
        //查询账户表
       // TronAccount tronAccount = tronAccountService.getTronAccount(payOrder.getAddress(), payOrder.getPrivateKey());
    }

    /**
     * 订单任务失败时更新的状态
     *
     * @param tonPayOrder
     */
    @Transactional
    public void updateFailStatus(TonPayOrder tonPayOrder) {
        if (Objects.isNull(tonPayOrder)) {
            log.error("更新失败状态异常！payOrder不能为空");
        }

        tonPayOrder.setStatus(-1); //将状态设置为未完成
        updatePayOrder(tonPayOrder);

        ///String address = tonPayOrder.getAddress();
    }

    /**
     * 获取未完成的所有订单 对象
     *
     * @return 返回List
     */
    public List<TonPayOrder> getUndonePayOrder() {
        return myTemplate.myQueryForListAppendWhereCondition(TonPayOrder.class, "status=?", 0);
    }
}
